#include "librarydialog.h"

#include "../appsettings.h"

#include <QtCore/qdir.h>
#include <QtGui/qboxlayout.h>
#include <QtGui/qdialogbuttonbox.h>
#include <QtGui/qfiledialog.h>
#include <QtGui/qformlayout.h>
#include <QtGui/qgroupbox.h>
#include <QtGui/qlineedit.h>
#include <QtGui/qpushbutton.h>

LibraryDialog::LibraryDialog(QWidget *parent) :
    QDialog(parent)
{
    setWindowTitle(tr("Create Library"));

    m_libName = new QLineEdit;
    connect(m_libName, SIGNAL(textEdited(QString)), this, SLOT(fileNameUpdate(QString)));

    m_fileName = new QLineEdit;

    m_fileDir = new QLineEdit(g_appSettings->asString("Library/Dir", QDir::currentPath()));
    connect(m_fileDir, SIGNAL(textEdited(QString)), this, SLOT(validate()));

    QPushButton *browseDir = new QPushButton(tr("Browse..."));
    connect(browseDir, SIGNAL(clicked()), this, SLOT(fileDirBrowse()));

    QHBoxLayout *dirLayout = new QHBoxLayout;
    dirLayout->addWidget(m_fileDir);
    dirLayout->addWidget(browseDir);

    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
                                     | QDialogButtonBox::Cancel);
    //m_buttonBox->button(QDialogButtonBox::Ok)->setDisabled(true);

    connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

    QFormLayout *generalLayout = new QFormLayout;
    generalLayout->addRow(tr("Library name:"), m_libName);
    generalLayout->addRow(tr("File name:"), m_fileName);
    generalLayout->addRow(tr("File directory:"), dirLayout);
    QGroupBox *generalGroup = new QGroupBox(tr("General"));
    generalGroup->setLayout(generalLayout);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(generalGroup);
    mainLayout->addWidget(m_buttonBox);
    setLayout(mainLayout);

    validate();
}

QString LibraryDialog::directory()
{
    return m_fileDir->text();
}

void LibraryDialog::fileDirBrowse()
{
    QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
    QString dir = QFileDialog::getExistingDirectory(this,
                                                    tr("Library Directory"),
                                                    m_fileDir->text(),
                                                    options);
    if (!dir.isEmpty())
    {
        m_fileDir->setText(dir);
        validate();
    }
}

void LibraryDialog::fileNameUpdate(const QString &libName)
{
    QString fileName;

    if (!libName.isEmpty())
    {
        fileName = libName.simplified().toLower();
        fileName.replace(' ', '_');
        fileName.replace(QRegExp("[\\\\/:*\?\"<>|]"), "");

        fileName += ".qlib";
    }
    m_fileName->setText(fileName);
    validate();
}

QString LibraryDialog::fullPath()
{
    QString fullPath = m_fileDir->text();
    if (!fullPath.endsWith('/'))
        fullPath += '/';
    fullPath += m_fileName->text();
    return fullPath;
}

QString LibraryDialog::name()
{
    return m_libName->text();
}

void LibraryDialog::validate()
{
    bool isValid = true;
    isValid = isValid && (!m_fileName->text().isEmpty());
    if (!QDir(m_fileDir->text()).exists())
    {
        isValid = false;
        m_fileDir->setStyleSheet("color: red");
    }
    else
        m_fileDir->setStyleSheet("");
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(isValid);
}
